﻿using System;
using System.Collections;
using System.Configuration;
using System.Web.UI;
using Antlr3.ST;
using WebApplication1.Classes;

namespace WebApplication1 {
    public abstract class BasePage : Page {
        public virtual Boolean SafePage { get { return true; } }

        public StringTemplateGroup StringTemplateGroup { set; get; }
        public Hashtable AttributesTemplate { set; get; }

        public abstract String TemplateName {
            get;
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            if (SafePage && Session["usuarioConectado"] == null)
                Response.Redirect("default.aspx?errorSeguridad=1", true);
            AttributesTemplate = new Hashtable();

            var path = Server.MapPath("~") + @"\templates\";

            //var path = "c:\\inetpub\\wwwroot\\SIC\\templates\\";
            StringTemplateGroup = new StringTemplateGroup("templateDir", path);
            StringTemplateGroup.RegisterRenderer(typeof(DateTime), new AdvancedDateTimeRenderer());
            SetGeneralDataAttributes();
        }

        private void SetGeneralDataAttributes() {
            AttributesTemplate.Add("usuarioConectado", Session["usuarioConectado"]);
            AttributesTemplate.Add("sucursal", Session["sucursal"]);
            AttributesTemplate.Add("nombreSistema", ConfigurationManager.AppSettings["nombreSistema"]);
        }

        protected override void Render(HtmlTextWriter writer) {
            base.Render(writer);
            var template = StringTemplateGroup.GetInstanceOf(TemplateName);
            foreach (DictionaryEntry entry in AttributesTemplate)
                template.SetAttribute(entry.Key.ToString(), entry.Value);

            Response.Write(template.ToString());
        }
    }
}
